home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / obsolete / contingent.pro < prev    next >
Text File  |  1997-07-08  |  4KB  |  186 lines

  1. ; $Id: contingent.pro,v 1.2 1997/01/15 04:02:19 ali Exp $
  2. ;
  3. ;  Copyright (c) 1991-1997, Research Systems Inc.  All rights
  4. ;  reserved. Unauthorized reproduction prohibited.
  5.  
  6.  
  7. Pro   MakeTable, A,Table,ColNames,RowNames,R,C,RT,CT,unit
  8.  
  9.   printf,unit,Format='(A15,$)',"               "     
  10.  
  11.   for i=0L,C-1 DO                 $
  12.    printf,unit,Format='(A11,$)',ColNames(i) 
  13.  
  14.    printf,unit,"    Total"
  15.   
  16.  
  17.    printf,unit,Format='(A15,$)',"               "     
  18.   for j=1L,C*11+1 do printf,unit,Format='(A1,$)',"_"
  19.   printf,unit," "
  20.  
  21.   for i=0L,R-1 do Begin
  22.     printf,unit,Format ='(A5,$)',"     "
  23.     for j=0L,C DO  printf,unit,Format='(A11,$)',"          |"
  24.     printf,unit," "
  25.     printf,unit,Format ='(A2,$)',"  "
  26.     printf,unit,Format='(A10,A3,A1,$)',RowNames(i),"   ","|"
  27.     for j= 0L,C-1 DO printf,unit,Format='(F10.2,A1,$)',    $
  28.                                         A(j,i),"|"
  29.     printf,unit,Format='(F8.2)',RT(i)
  30.     printf,unit,Format ='(A16,$)',"               |"
  31.     for j= 0L,C-1 DO printf,unit,Format='(A1,F8.3,A1,A1,$)',$
  32.                                        "(",Table(j,i),")","|"
  33.     printf,unit," "
  34.     printf,unit,Format='(A15,$)',"               "     
  35.     for j=1L,C*11+1 do printf,unit,Format='(A1,$)', "_"
  36.     printf,unit," "
  37.   ENDFOR
  38.   
  39.   printf,unit,Format='(A3,A10,A2,$)',"  ","Total","  "
  40.   for i = 0L,C-1 Do  printf,unit,Format='(F10.2,A1,$)',    $
  41.                           CT(i)," "
  42.   printf,unit,Format='(F10.2)',Total(CT)
  43.  
  44.   printf,unit,"  "
  45.   
  46.  
  47.   RETURN
  48.   END
  49.       
  50.  
  51.  
  52.  
  53.  
  54.  PRO Contingent, X, ChiSqr, Prob, DF, ColNames= ColNames, RowNames=RowNames,$
  55.          List_Name= LN
  56. ;+
  57. ;
  58. ; NAME:
  59. ;    CONTINGENT
  60. ;
  61. ; PURPOSE:
  62. ;    Construct a two-way contingency table from the count data in X and 
  63. ;    test for independence between two factors represented by the rows 
  64. ;    and columns of X.
  65. ;
  66. ; CATEGORY:
  67. ;    Statistics.
  68. ;
  69. ; CALLING SEQUENCE:
  70. ;    Contingent, X, ChiSqr, Prob, DF
  71. ; INPUTS: 
  72. ;    X:    input array of count data. X(i,j) is the number
  73. ;        of observations at level i and j of the column
  74. ;        and row factors respectively.
  75. ;
  76. ; OUTPUT:
  77. ;    Contingency table writtem to the screen.
  78. ;
  79. ; OPTIONAL OUTPUT PARAMETERS:      
  80. ;    ChiSqr:    the statistic to test for factor independence.
  81. ;
  82. ;    Prob:    the probability of ChiSqr or something larger from a chi 
  83. ;        square distribution.
  84. ;
  85. ;    DF:    degrees of freedom
  86. ;
  87. ; KEYWORDS:
  88. ;     COLNAMES:    vector of names to label table columns.
  89. ;
  90. ;     ROWNAMES:    vector of names to label table rows.
  91. ;
  92. ;    LIST_NAME:    name of output file. default is to the screen.
  93. ;                          
  94. ; RESTRICTIONS:
  95. ;    None.
  96. ;
  97. ; COMMON BLOCKS:
  98. ;    None.
  99. ;
  100. ; SIDE EFFECTS:
  101. ;    None.
  102. ;
  103. ; PROCEDURE:
  104. ;    Calculation of standard formulas to compute ChiSqr.
  105. ;-
  106.  
  107. On_Error,2
  108. SX=Size(X)             ; Get Dimensions
  109.  
  110. if ( N_Elements(LN) NE 0) THEN openw,unit,/Get,LN    $
  111. ELSE unit = -1
  112.  
  113.  
  114. if(SX(0) NE 2) THEN BEGIN
  115.  printf,unit,            $
  116.           'Contingent- Need 2- dimensional array for Table'
  117.  goto, DONE
  118. ENDIF
  119.  
  120.  sn = where(X LT 0,count)
  121.  
  122. if count gt 0 THEN BEGIN
  123.   printf,unit,'Contingent- Data should be positive.'
  124.   goto,DONE
  125. ENDIF
  126.  
  127.  C=SX(1)                ; # of Columns
  128.  R=SX(2)                ; # of Rows
  129.  
  130.  SC= N_Elements(ColNames)
  131.  SR= N_Elements(RowNames)
  132.  
  133.  if ( SC EQ 0) THEN BEGIN
  134.    ColNames = ['Col1']
  135.    SC = SC + 1
  136.  ENDIF
  137.  
  138.  if ( SC LT C) THEN  $
  139.    for i = SC,C-1 DO                    $
  140.  ColNames = [ColNames,'Col' + StrTrim( i+1,2)]
  141.  
  142.  if ( SR EQ 0) THEN BEGIN
  143.    RowNames = ['Row1']
  144.    SR = SR + 1
  145.  ENDIF
  146.  
  147.  if ( SR LT R) THEN  $
  148.    for i = SR,R-1 DO           $
  149.      RowNames = [RowNames,'Row' + StrTrim( i+1,2)]
  150.  
  151.  GrandTotal=Total(X)
  152.  ColTotal = X#(FltArr(R)+1)
  153.  RowTotal =  (FltArr(C)+1)#X
  154.  if GrandTotal ne 0 then $
  155.    Table=ColTotal#RowTotal/GrandTotal $
  156.  else  $
  157.    message,' Must Halt since counts are all 0'
  158.  
  159.  DF=(r-1)*(c-1)
  160.  X2=X-Table
  161.  
  162.  ChiSqr= Total((X2*X2)/Table)
  163.  Prob = 1 -chi_sqr1(ChiSqr,DF)
  164.  
  165.  MakeTable,X,Table,ColNames,RowNames,R,C,RowTotal,ColTotal,$
  166. unit
  167.  
  168.  printf,unit," "
  169.  printf, unit,format =     $
  170.        '("ChiSqr=",G15.8, "   Probability =",G15.8)',  $
  171.  ChiSqr, Prob
  172.  
  173.  DONE:
  174.  if (unit NE -1) THEN Free_Lun,unit
  175.  
  176.  RETURN
  177.  END
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.